home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-19 | 3.9 KB | 147 lines | [TEXT/CWIE] |
- // =================================================================================
- // GreyGWorld.cp ©1997 BB's Team inc. All rights reserved
- // =================================================================================
- #include "GreyGWorld.h"
-
-
- // ---------------------------------------------------------------------------------
- // • default ctor
- // ---------------------------------------------------------------------------------
- GreyGWorld::GreyGWorld (void)
- : mGW (nil), mPM (nil), mLock (0)
- {}
-
-
- // ---------------------------------------------------------------------------------
- // • Rect ctor
- // ---------------------------------------------------------------------------------
- GreyGWorld::GreyGWorld (Rect &r)
- : mGW (nil), mPM (nil), mLock (0)
- {
- SetRect (r);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetRect
- // ---------------------------------------------------------------------------------
- void GreyGWorld::SetRect (Rect&r)
- {
- MakeRoom();
-
- // Get a gray palette (from white to black)
- CTabHandle theColorTable;
- theColorTable = (CTabHandle) ::GetResource ('clut', 128);
- if (!theColorTable)
- return;
-
- // Build the offscreen
- GWorldPtr theGW;
- ::NewGWorld (&theGW, 8, &r, theColorTable, nil ,0);
- ::ReleaseResource ( (Handle)theColorTable );
-
- if ( theGW!=nil ) {
- mGW = theGW;
- mPM = ::GetGWorldPixMap (mGW);
- mStep = (**mPM).rowBytes & 0x7fff;
- }
-
- }
-
-
- // ---------------------------------------------------------------------------------
- // • dtor
- // ---------------------------------------------------------------------------------
- GreyGWorld::~GreyGWorld ()
- {
- MakeRoom();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • MakeRoom
- // ---------------------------------------------------------------------------------
- void GreyGWorld::MakeRoom (void)
- {
- if ( IsOK() ) {
- Unlock();
- DisposeGWorld (mGW);
- mGW = nil;
- mPM =nil;
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Lock
- // ---------------------------------------------------------------------------------
- void GreyGWorld::Lock(void)
- {
- if ( IsOK() && !mLock )
- ::LockPixels (mPM);
- mLock++;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Unlock
- // ---------------------------------------------------------------------------------
- void GreyGWorld::Unlock(void)
- {
- mLock--;
- if (mLock < 0)
- mLock=0;
- if (IsOK() && mLock!=0)
- ::UnlockPixels (mPM);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • IsOK
- // ---------------------------------------------------------------------------------
- Boolean GreyGWorld::IsOK (void)
- {
- return mGW!=nil;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Bounds
- // ---------------------------------------------------------------------------------
- Rect GreyGWorld::Bounds(void)
- {
- return (**mPM).bounds;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ComputeGrey
- // ---------------------------------------------------------------------------------
- float GreyGWorld::ComputeGrey (void)
- {
- return ComputeGrey ((**mPM).bounds);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ComputeGrey
- // ---------------------------------------------------------------------------------
- float GreyGWorld::ComputeGrey (Rect &r)
- {
- // address of the leftmost pixel in the pixmap of
- // the line alined with the top of the Rect (sic)
- unsigned char *p = (unsigned char *) GetPixBaseAddr (mPM) + mStep*r.top;
-
- Int32 theSum = 0;
-
- for (Int16 y = r.top ; y<r.bottom ; y++) {
- for (Int16 x = r.left ; x<r.right ; x++)
- theSum += p[x];
- p += mStep;
- }
-
- // The grey ramp used for the palette goes from white to black
- // We have computed the inverse of the lightness
- return 1. - theSum/(255.*(r.right-r.left)*(r.bottom-r.top));
- }
-